#ifndef _QANSIWIDGET_H
#define _QANSIWIDGET_H


#include "qansieditor.h"
#include "qae_global.h"
#include <QtGui/QWidget>


class QTextCodec;
class QTableWidgetItem;


namespace QAnsiEditor {


class QEditorWindow;
class QAnsiBuffer;
class QAnsiDecoder;


class QAnsiWidget : public QWidget
{
    Q_OBJECT

public:
    QAnsiWidget(QWidget *parent, int iRow, int iCol);
    ~QAnsiWidget();
    void setContent(QByteArray bytes, QGlobal::Encoding encoding);
    QByteArray getContent(QGlobal::Encoding encoding);  // used in save

public slots:
    void newFile();
    void open();
    void save();
    void saveAs();
    void undo();
    void redo();
    void cut();
    void copy();
    void paste();
    void colorChanged(char chColor);
    void blinkToggled(bool bBlink);
    void underlineToggled(bool bUnderline);
    void charClicked(QTableWidgetItem *item);

protected:
    bool event(QEvent *e);
    void showEvent(QShowEvent *se);
    void paintEvent(QPaintEvent *pe);
    void resizeEvent(QResizeEvent *re);
    void keyPressEvent(QKeyEvent *ke);
    void mousePressEvent(QMouseEvent *me);
    void mouseMoveEvent(QMouseEvent *me);
    void mouseReleaseEvent(QMouseEvent *me);
    void inputMethodEvent(QInputMethodEvent *e);

    void drawSelection();
    void drawContent();
    void drawCursor();


private slots:
    void blinkEvent();
    void cursorBlinkEvent();

private:

    enum RenderFlag {
        RenderLeft,
        RenderRight,
        RenderAll
    };

    QRect m_rcClient;
    QColor m_arrColors[16];

    QTimer *m_pBlinkTimer;
    QTimer *m_pCursorBlinkTimer;

    bool m_bBlinkVisable;
    bool m_bCursorBlinkEnabled;
    bool m_bCursorVisable;

    QFont m_asciiFont;
    QFont m_nonAsciiFont;
    int m_iCharAscent;
    int m_iCharDescent;
    int m_iCharWidth;
    int m_iCharHeight;
    int m_iCharDelta;
    int m_iLineSpacing; // for future

    char m_chCurrentColor;
    char m_chCurrentEffect;

    int m_iRow;
    int m_iCol;
    QTextCodec *m_pInputCodec;
    QTextCodec *m_pOutputCodec;
    QAnsiBuffer *m_pBuffer;

    // mouse select
    bool m_bSelecting;
    QPoint m_ptSelStart;
    QPoint m_ptSelEnd;

    bool m_bCopyWithColor;
    QRect m_rcSelection;
    QPoint m_ptCursor;
    int m_nScreenStart;

private:
    QPoint mapCharToPixel(const QPoint &ptChar);
    QPoint mapPixelToChar(const QPoint &ptPixel);
    QRect mapCharToPixelRect(int iX, int iY, int iWidth, int iHeight);
    QRect mapCharToPixelRect(const QRect &rcPixel);
    void inputHandle(const QString &text);
    void initColors();
    void initFonts();
    void updateFontSize();
    void calcFontMetrics();
    void setCursorBlinkEnabled(bool bEnable);

    // display
    void drawChar(QPainter &painter, const QString &strChar,
                  int iX, int iY, int iWidth, char chColor, char chEffect, RenderFlag flags);
    void drawLine(QPainter &painter, int iIndex, int iStartX=0, int iEndX=-1);

    // by gonwan
    static QByteArray parseString(const QByteArray&, /*out*/ int *len = 0);

};


}  // namespace QAnsiEditor


#endif  // _QANSIWIDGET_H
